<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<script src="<?php echo $this->getSkinUrl('js/jquery-1.2.2.pack.js') ?>" type="text/javascript"></script>
<link href="<?php echo $this->getSkinUrl('css/facebox.css') ?>" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    var closebtn = "<?php echo $this->getSkinUrl('images/closelabel.gif') ?>";
</script>
<script src="<?php echo $this->getSkinUrl('js/facebox.js') ?>" type="text/javascript"></script>

<script type="text/javascript">
     var eff = jQuery.noConflict();
    jQuery(document).ready(function(eff) {
      eff('a[rel*=facebox]').facebox()
    })
</script>

<?php  $_order = $this->getOrder() ?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<div class="page-title title-buttons">
    <h1><?php echo $this->__('Order #%s - %s', $_order->getRealOrderId(), $_order->getStatusLabel()) ?></h1>
    <?php if($this->helper('rss/order')->isStatusNotificationAllow()): ?>
        <a href="<?php echo $this->helper('rss/order')->getStatusHistoryRssUrl($_order) ?>" class="link-rss"><?php echo $this->__('Subscribe to Order Status') ?></a>
        <span class="separator">|</span>
    <?php endif; ?>
   
	
    <a href="<?php echo $this->getPrintUrl($_order) ?>" class="link-print" onclick="this.target='_blank';"><?php echo $this->__('Print Order') ?></a>
	
<?php
$baseUrl = Mage::getBaseUrl();
$order = Mage::getModel('sales/order')->load($_order->getId());
$items = $order->getAllItems();
$itemcount=count($items);
$Productname='';
$unitPrice='';
$qty='';
$orderStatus= $order->getStatus();
foreach ($items as $itemId => $item) {

    $model = Mage::getModel('catalog/product');
    $productId = $item->getProductId();
	$orderedQty = $item->getQtyOrdered();

	$productdetail = $model->load($productId);
	$target = $productdetail->gettarget_number();
	}
        $resource = Mage::getSingleton('core/resource');
        $read = $resource->getConnection('catalog_read');
        $orderTable = $resource->getTableName('sales/order');
        $orderItemTable = $resource->getTableName('sales/order_item');
        $dealstatus[0] = "processing";
        //$dealstatus[1] = "pending";
        $dealstatus[2] = "complete";
        $select = $read->select()
                ->from(array('cp'=>$orderTable),array( 'totalcount'  => 'sum(cp.total_qty_ordered)','orderid' => 'pei.order_id','firstname' => 'cp.customer_firstname','lastname' => 'cp.customer_lastname'))
                ->join(array('pei'=>$orderItemTable), 'pei.order_id=cp.entity_id',array())
                ->where('cp.status in (?)', $dealstatus)
                ->where('pei.product_id in (?)', $productId);
								
				if($select->count)				:
	        $orders_list = $read->fetchAll($select);
				endif;
        $quantity[0] = floor($orders_list[0]['totalcount']);
        $orderid = $orders_list[0]['orderid'];
        $customer_name = $orders_list[0]['firstname'].' '.$orders_list[0]['lastname'];

         /*
         * checking for order status.
         */
        if(in_array($orderStatus,$dealstatus)){
            $canPrintCoupon=1;
        }
        $giftemail = $resource->getConnection('core_write');
 $tprefix = (string)Mage::getConfig()->getTablePrefix();
$giftCouponCheck = $giftemail->fetchRow("select * from ".$tprefix."ordercustomer where order_id ='$orderid' and customer_name = '$customer_name'");

							if(isset($giftCouponCheck['uniqueid'])) {
if($target <= $quantity[0] && isset($canPrintCoupon))
	   {
	   ?>
	   <span class="separator">|</span>
	   <!--<a href="<?php if($orderedQty ==1) { echo $this->getCouponUrl($_order) ; } else { echo $this->getCertificateUrl($_order); } ?>" class="link-print" onclick="this.target='_blank';"><?php echo $this->__('Print Certificate') ?></a>-->
<?php if($orderedQty ==1) {?>
           <a href=" <?php echo $this->getCouponUrl($_order) ;  ?>" class="link-print" onclick="this.target='_blank';"><?php echo $this->__('Print Certificate') ?></a> | <a href="<?php echo $baseUrl;?>sales/order/coupon/order_id/<?php echo $orderid;?>/pdf/1" onclick="this.target='_blank';" title="Download pdf"><?php echo $this->__('Download') ?></a>
<? } else { ?>
<a href="#printcoupon" class="link-print" rel="facebox"><?php echo $this->__('Print Certificate') ?></a>
	   <?php
}
	   } 	}
	?>
	
</div>
<?php echo $this->getStatusHistoryRssUrl($_order) ?>
<dl class="order-info">
    <dt><?php echo $this->__('About This Order:') ?></dt>
    <dd>
        <?php $_links = $this->getLinks(); ?>
        <ul id="order-info-tabs">
        <?php foreach ($_links as $_link): ?>
            <?php if($_link->getUrl()): ?>
                <li><a href="<?php echo $_link->getUrl() ?>"><?php echo $_link->getLabel() ?></a></li>
            <?php else: ?>
                <li class="current"><?php echo $_link->getLabel() ?></li>
            <?php endif; ?>
        <?php endforeach; ?>
        </ul>
        <script type="text/javascript">decorateGeneric($('order-info-tabs').select('LI'),['first','last']);</script>
    </dd>
</dl>
<p class="order-date"><?php echo $this->__('Order Date: %s', $this->formatDate($_order->getCreatedAtStoreDate(), 'long')) ?></p>
<?php if (!$_order->getIsVirtual()): ?>
<div class="col2-set order-info-box">
    <div class="col-1">
        <div class="box">
            <div class="box-title">
                <h2><?php echo $this->__('Shipping Address') ?></h2>
            </div>
            <div class="box-content">
                <address><?php echo $_order->getShippingAddress()->format('html') ?></address>
            </div>
        </div>
    </div>
    <div class="col-2">
        <div class="box">
            <div class="box-title">
                <h2><?php echo $this->__('Shipping Method') ?><?php /* | <a href="#"><?php echo $this->__('Track Order') ?></a>*/ ?></h2>
            </div>
            <div class="box-content">
                <?php if ($_order->getShippingDescription()): ?>
                    <?php echo $_order->getShippingDescription() ?>
                <?php else: ?>
                    <p><?php echo $this->helper('sales')->__('No shipping information available'); ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>
<div class="col2-set order-info-box">
    <div class="col-1">
        <div class="box">
            <div class="box-title">
                <h2><?php echo $this->__('Billing Address') ?></h2>
            </div>
            <div class="box-content">
                <address><?php echo $_order->getBillingAddress()->format('html') ?></address>
            </div>
        </div>
    </div>
    <div class="col-2">
        <div class="box box-payment">
            <div class="box-title">
                <h2><?php echo $this->__('Payment Method') ?></h2>
            </div>
            <div class="box-content">
                <?php echo $this->getPaymentInfoHtml() ?>
            </div>
        </div>
    </div>
</div>


<!-- Print multiple coupon -->
<?php $baseUrl = Mage::getBaseUrl();?>
<div class="about_topcontent page-title" style="text-align:left; display:none;" id="printcoupon">
    <h1><?php echo $this->__('Print Your Certificates') ?>

        <?php $orderId = (int)$this->getRequest()->getParam('order_id');

        $order = Mage::getModel('sales/order')->load($orderId);
        $incrementId = $order->getIncrementId();
        $items = $order->getAllItems();

        foreach ($items as $itemId => $item) { $ordertotal = $item->getQtyOrdered(); }


        echo '<span style="float:right">#'.$order->getIncrementId().'</span>';

        $resource = Mage::getSingleton('core/resource');
        $read = $resource->getConnection('catalog_read');
        $orderTable = $resource->getTableName('sales/order');
        $orderItemTable = $resource->getTableName('sales/order_item');

        $select = $read->select()
        ->from(array('cp'=>$orderTable),array( 'name'  => 'pei.name'))
        ->join(array('pei'=>$orderItemTable), 'pei.order_id=cp.entity_id',array())
        ->where('pei.order_id in (?)', $orderId);
        $deallist = $read->fetchAll($select);
        $dealName = $deallist[0]['name'];
        ?>
    </h1>
    <div><img src="<?php echo $this->getUrl('media').'titlebottom.jpg'?>" /></div>
    <h3><?php echo $dealName;?></h3><br/>
    <ul class="clsprint_certificate clearfix">
        <?php for($i=0;$i < $ordertotal;$i++)
        {?>
       <li class="floatleft" style="padding:10px 15px;"><span class="floatleft" style="padding:0 10px; display: block; font-weight: bold;"><?php echo $this->__('Coupon') ?> #<?php echo ($i+1);?></span>
         <ul class="floatleft">
             <li style="display:inline;"><a href="<?php echo $baseUrl;?>sales/order/coupon/order_id/<?php echo $orderId;?>/id/<?php echo $i+1;?>" class="link-print" onclick="this.target='_blank';"><?php echo $this->__('Print') ?></a></li>
             <li style="display:inline;">|</li>
             <li style="display:inline;"><a href="<?php echo $baseUrl;?>sales/order/coupon/order_id/<?php echo $orderId;?>/id/<?php echo $i+1;?>/pdf/1" onclick="this.target='_blank';" title="Download pdf"><?php echo $this->__('Download') ?><!--<img src="<?php echo $this->getSkinUrl('images/pdf.png') ?>"/>--></a></li>
         </ul>
        </li>
        <?php } ?>
    </ul>
</div>
